package com.tanhua.server.controller;

import com.tanhua.model.domain.User;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 获取登录验证码
     * 请求参数:phone(封装为map)
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map) {

        String phone = (String) map.get("phone");
        userService.sendMsg(phone);
        return ResponseEntity.ok(null); //ok代表正常返回，状态码200
    }

    /**
     * 登录校验
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        String code = (String) map.get("verificationCode");
        //2.调用userservice完成用户登录
        Map resultMap = userService.loginVerification(phone, code);
        //3.构造返回
        return ResponseEntity.ok(resultMap);
    }
}
